<?php

namespace app\admin\controller;

use app\model\PayPingModel;
use support\Request;
use support\Response;
use Exception;

/**
 * 点评管理控制器
 * 
 * @package app\admin\controller
 */
class PayPingController
{
    /**
     * 获取点评列表
     * 
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            
            $query = PayPingModel::query()
                ->with(['user', 'order', 'biz'])
                ->orderBy('id', 'desc');
                
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            foreach ($list as &$item) {
                $item['type_name'] = PayPingModel::TYPE[$item->type];
                $item['images'] = $item->doImages();

                if(in_array($item->type, ['biz-order', 'order'])){
                    $item['goods_name'] = $item->goods->title;
                }else{
                    $item['goods_name'] = '-';
                }
                
                // 确保回复字段存在
                $item['biz_reply'] = $item->biz_reply ?: '';
                $item['system_reply'] = $item->system_reply ?: '';
            }
                
            return json([
                'data' => [
                    'list' => $list,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取点评详情
     * 
     * @param Request $request
     * @return Response
     */
    public function detail(Request $request): Response
    {
        try {
            $id = (int)$request->get('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $payPing = PayPingModel::query()
                ->with(['user', 'order', 'biz'])
                ->find($id);

            if (!$payPing) {
                throw new Exception('点评不存在');
            }

            $payPing['images'] = $payPing->doImages();
            $payPing['biz_reply'] = $payPing->biz_reply ?: '';
            $payPing['system_reply'] = $payPing->system_reply ?: '';
                
            return json([
                'data' => $payPing,
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 商家回复
     * 
     * @param Request $request
     * @return Response
     */
    public function bizReply(Request $request): Response
    {
        try {
            $id = (int)$request->post('id');
            $reply = $request->post('reply');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            if (empty($reply)) {
                throw new Exception('回复内容不能为空');
            }

            $payPing = PayPingModel::find($id);
            if (!$payPing) {
                throw new Exception('点评不存在');
            }

            $payPing->biz_reply = $reply;
            $payPing->updated_at = date('Y-m-d H:i:s');
            
            if (!$payPing->save()) {
                throw new Exception('回复失败');
            }
                
            return json([
                'data' => null,
                'msg' => '回复成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 平台回复
     * 
     * @param Request $request
     * @return Response
     */
    public function systemReply(Request $request): Response
    {
        try {
            $id = (int)$request->post('id');
            $reply = $request->post('reply');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            if (empty($reply)) {
                throw new Exception('回复内容不能为空');
            }

            $payPing = PayPingModel::find($id);
            if (!$payPing) {
                throw new Exception('点评不存在');
            }

            $payPing->system_reply = $reply;
            $payPing->updated_at = date('Y-m-d H:i:s');
            
            if (!$payPing->save()) {
                throw new Exception('回复失败');
            }
                
            return json([
                'data' => null,
                'msg' => '回复成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除点评
     * 
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        try {
            $id = (int)$request->post('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $payPing = PayPingModel::find($id);
            if (!$payPing) {
                throw new Exception('点评不存在');
            }

            $result = $payPing->delete();
            if (!$result) {
                throw new Exception('删除失败');
            }
                
            return json([
                'data' => null,
                'msg' => '删除成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 